﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET.Monitor.DAL
{
    public class FinanceDAL
    {
        string strSql = "", strErrMsg = "";
        object obj = null;
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        DataTable dtList = new DataTable();

        InvokeWebService iw = new InvokeWebService();
        string rtdbRelationAddress = "";
        string rtdbRealTimeAddress = "";
        string rtdbNameSpace = "";
        string rtdbJudge = "";
        public FinanceDAL()
        {
            rtdbRelationAddress = ConfigurationManager.ConnectionStrings["RelationURL"].ConnectionString;
            rtdbRealTimeAddress = ConfigurationManager.ConnectionStrings["RealTimeURL"].ConnectionString;
            rtdbNameSpace = ConfigurationManager.ConnectionStrings["NameSpace"].ConnectionString;
            rtdbJudge = ConfigurationManager.ConnectionStrings["Judge"].ConnectionString;
        }

        #region 运维费用支出
        public bool AddOMCost(string orgId, string typeId, float paymentAmount, string time, byte[] b, string fileName)
        {
            dtList = new DataTable();
            dtList.Columns.Add("key");
            dtList.Columns.Add("value");

            DataRow dr = dtList.NewRow();

            dr = dt.NewRow();
            dr["key"] = "T_ORGID";
            dr["value"] = orgId;
            dtList.Rows.Add(dr);

            dr = dt.NewRow();
            dr["key"] = "T_TYPEID";
            dr["value"] = typeId;
            dtList.Rows.Add(dr);

            dr = dt.NewRow();
            dr["key"] = "D_PAYMENTAMOUNT";
            dr["value"] = paymentAmount;
            dtList.Rows.Add(dr);

            dr = dt.NewRow();
            dr["key"] = "T_TIME";
            dr["value"] = time;
            dtList.Rows.Add(dr);


            if (!string.IsNullOrEmpty(fileName))
            {
                dr = dt.NewRow();
                dr["key"] = "T_ATTACHMENTNAME";
                dr["value"] = fileName;
                dtList.Rows.Add(dr);

                dr = dt.NewRow();
                dr["key"] = "B_ATTACHMENT";
                dr["value"] = b;
                dtList.Rows.Add(dr);
            }
            ds = new DataSet();
            ds.Tables.Add(dtList);

            //List<Hashtable> list = new List<Hashtable>();
            //Hashtable ht = new Hashtable();
            //ht.Add("key", "T_ORGID");
            //ht.Add("value", orgId);
            //list.Add(ht);
            //ht = new Hashtable();
            //ht.Add("key", "T_TYPEID");
            //ht.Add("value", typeId);
            //list.Add(ht);
            //ht = new Hashtable();
            //ht.Add("key", "D_PAYMENTAMOUNT");
            //ht.Add("value", paymentAmount);
            //list.Add(ht);
            //ht = new Hashtable();
            //ht.Add("key", "T_TIME");
            //ht.Add("value", time);
            //list.Add(ht);

            //if (!string.IsNullOrEmpty(fileName))
            //{
            //    ht = new Hashtable();
            //    ht.Add("key", "T_ATTACHMENTNAME");
            //    ht.Add("value", fileName);
            //    list.Add(ht);
            //}

            bool bl = (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "UpDocument", new object[] { ds, "T_INFO_OMCOST", "B_ATTACHMENT", b });
            //bool bl = db.UpDocument(list, "T_INFO_OMCOST", "B_ATTACHMENT", b);



            return bl;
        }

        public bool UpdateOMCost(string idKey, string typeId, float paymentAmount, string time, byte[] bytes, string fileName)
        {
            dtList = new DataTable();
            dtList.Columns.Add("key");
            dtList.Columns.Add("value");

            DataRow dr = dtList.NewRow();

            dr = dt.NewRow();
            dr["key"] = "T_TYPEID";
            dr["value"] = typeId;
            dtList.Rows.Add(dr);

            dr = dt.NewRow();
            dr["key"] = "D_PAYMENTAMOUNT";
            dr["value"] = paymentAmount;
            dtList.Rows.Add(dr);

            dr = dt.NewRow();
            dr["key"] = "T_TIME";
            dr["value"] = time;
            dtList.Rows.Add(dr);

            if (bytes != null && bytes.Length != 0 && fileName != "")
            {
                dr = dt.NewRow();
                dr["key"] = "B_ATTACHMENT";
                dr["value"] = bytes;
                dtList.Rows.Add(dr);

                dr = dt.NewRow();
                dr["key"] = "T_ATTACHMENTNAME";
                dr["value"] = fileName;
                dtList.Rows.Add(dr);
            }

            DataSet dsWhere = new DataSet();
            ds.Tables.Add(dtList);

            DataTable dtWhere = new DataTable();
            dtWhere.Columns.Add("key");
            dtWhere.Columns.Add("value");
            DataRow drWhere = dtWhere.NewRow();
            drWhere["key"] = "ID_KEY";
            drWhere["value"] = idKey;
            dtWhere.Rows.Add(drWhere);
            dsWhere.Tables.Add(dtWhere);

            //List<Hashtable> list = new List<Hashtable>();
            //List<Hashtable> listWhere = new List<Hashtable>();

            //Hashtable ht = new Hashtable();
            //ht.Add("key", "T_TYPEID");
            //ht.Add("value", typeId);
            //list.Add(ht);
            //ht = new Hashtable();
            //ht.Add("key", "D_PAYMENTAMOUNT");
            //ht.Add("value", paymentAmount);
            //list.Add(ht);
            //ht = new Hashtable();
            //ht.Add("key", "T_TIME");
            //ht.Add("value", time);
            //list.Add(ht);
            //if (bytes != null && bytes.Length != 0 && fileName != "")
            //{
            //    ht = new Hashtable();
            //    ht.Add("key", "B_ATTACHMENT");
            //    ht.Add("value", bytes);
            //    list.Add(ht);
            //    ht = new Hashtable();
            //    ht.Add("key", "T_ATTACHMENTNAME");
            //    ht.Add("value", fileName);
            //    list.Add(ht);
            //}
            //ht = new Hashtable();
            //ht.Add("key", "ID_KEY");
            //ht.Add("value", idKey);
            //listWhere.Add(ht);

            bool bl = (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "UpDocumentEdit", new object[] { ds, dsWhere, "T_INFO_OMCOST" });
            //bool bl = db.UpDocumentEdit(list, listWhere, "T_INFO_OMCOST");
            return bl;
        }

        public bool DeleteOMCost(string idKey)
        {
            strSql = @"DELETE FROM T_INFO_OMCOST WHERE ID_KEY=" + idKey;
            return (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { strSql, strErrMsg });
        }

        public DataTable GetOMCostType()
        {
            strSql = @"SELECT T_TYPEID ,T_TYPEDESC_EN ,T_TYPEDESC_CN FROM T_BASE_OMCOSTTYPE";
            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }

        #endregion
    }
}
